class roleControl extends eui.Component {
	public constructor() {
		super();
	}
	/**触摸球 */
	public handle: eui.Image = new eui.Image();
	/**圈 */
	private circle: eui.Image = new eui.Image();
	/**圈坐标 */
	public circle_position = { "x": 150 * DataProl.gameSceneScal, "y": (GlobalData.GameStage_height * DataProl.gameSceneScal - 150) };//圈中心点的位置
	/**触摸球坐标 */
	public handle_position = { "x": 150 * DataProl.gameSceneScal, "y": (GlobalData.GameStage_height * DataProl.gameSceneScal - 150) };//小球中心店的位置
	private touchID: any = null;//触摸id
	private circle_radius: number = 0;
	private handle_radius: number = 0;
	private jiaodu: number = 0;//返回角度
	private container: any;
	public gameMapss: any;
	public pos = new Vector_2();
	//联网发送位置定时器
	public sendMsgTimer: egret.Timer = new egret.Timer(1000 / 10, -1);

	private islock: boolean;
	/**触摸位置
	 * 1-屏幕左半边
	 * 2-屏幕右半边
	 */
	public touchLocation: number = 1;
	public yaoganInit(object: any) {//摇杆初始化
		this.container = object;

		this.handle.texture = RES.getRes("hand");
		this.addChild(this.handle);
		this.handle.anchorOffsetX = this.handle.width / 2;
		this.handle.anchorOffsetY = this.handle.width / 2;
		this.handle.x = this.handle_position.x;
		this.handle.y = this.handle_position.y;

		this.circle.texture = RES.getRes("handbg");
		this.addChild(this.circle);
		this.circle.anchorOffsetX = this.circle.width / 2;
		this.circle.anchorOffsetY = this.circle.width / 2;
		this.circle.x = this.circle_position.x;
		this.circle.y = this.circle_position.y;

		this.circle_radius = this.circle.width / 2;
		this.handle_radius = this.handle.width / 2;

		this.container.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.MoveHandle, this);
		this.container.addEventListener(egret.TouchEvent.TOUCH_END, this.recoverHandle, this);
		this.container.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.MoveHandle, this);
		this.gameMapss = webNetworkMananger.getInstance().gameSceneRec;
		//console.log("摇杆初始化完成");
		// this.timer.addEventListener(egret.TimerEvent.TIMER, this.sendPosition, this);
		// this.timer.start();
		//console.log("摇杆定时器初始化完成");
		if (webNetworkMananger.isServerMove) {
			this.sendMsgTimer.addEventListener(egret.TimerEvent.TIMER, this.webSendPosition, this);
			this.sendMsgTimer.start();
			//console.log("联网模式定时器初始化完成");
		}

	}
	/**改变大小可见范围 */
	public changeVis(scal) {
		this.circle_position = { "x": 150 * DataProl.gameSceneScal, "y": (GlobalData.GameStage_height * DataProl.gameSceneScal - 150) };//圈中心点的位置
		this.handle_position = { "x": 150 * DataProl.gameSceneScal, "y": (GlobalData.GameStage_height * DataProl.gameSceneScal - 150) };//小球中心店的位置
		this.handle.scaleX = this.handle.scaleY = scal;
		this.circle.scaleX = this.circle.scaleY = scal;
		// egret.Tween.get(this.handle).to({ scaleX: scal, scaleY: scal }, 500);
		// egret.Tween.get(this.circle).to({ scaleX: scal, scaleY: scal }, 500);
		// egret.Tween.get(this.handle).to({ x: this.handle_position.x, y: this.handle_position.y }, 500);
		// egret.Tween.get(this.circle).to({ x: this.circle_position.x, y: this.circle_position.y }, 500);


		this.handle.x = this.handle_position.x;
		this.handle.y = this.handle_position.y;

		this.circle.x = this.circle_position.x;
		this.circle.y = this.circle_position.y;
	}
	/**摇杆锁定 */
	public lockControl() {
		this.islock = true;
	}
	/**摇杆解锁 */
	public unlockControl() {
		this.islock = false;

	}


	/**摇杆移动 */
	private MoveHandle(evt: egret.TouchEvent) {
		if (this.islock) {
			return;
		}
		var distanceX = Math.abs(evt.stageX - this.circle_position.x);
		var distanceY = Math.abs(evt.stageY - this.circle_position.y);
		var distance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
		if (this.touchLocation == 1) {
			if (evt.stageX < gameGlobal.global.screenWidth / 2) {
				this.touchID = evt.touchPointID;
				var rotation = this.calculateRotation(this.circle_position, { "x": evt.stageX, "y": evt.stageY });
				var hudu = rotation * Math.PI / 180;
				if (distance < this.circle_radius) {
					this.handle.x = evt.stageX;
					this.handle.y = evt.stageY;
				} else {
					this.handle.x = this.circle_position.x + Math.sin(hudu) * (this.circle_radius);
					this.handle.y = this.circle_position.y - Math.cos(hudu) * (this.circle_radius);
				}
				this.jiaodu = Math.floor(rotation);
			}
		} else if (this.touchLocation == 2) {
			if (evt.stageX > gameGlobal.global.screenWidth / 2) {
				this.touchID = evt.touchPointID;
				var rotation = this.calculateRotation(this.circle_position, { "x": evt.stageX, "y": evt.stageY });
				var hudu = rotation * Math.PI / 180;
				if (distance < this.circle_radius) {
					this.handle.x = evt.stageX;
					this.handle.y = evt.stageY;
				} else {
					this.handle.x = this.circle_position.x + Math.sin(hudu) * (this.circle_radius);
					this.handle.y = this.circle_position.y - Math.cos(hudu) * (this.circle_radius);
				}
				this.jiaodu = Math.floor(rotation);
			}
		}


	}
	private handleTween(x: any, y: any) {
		egret.Tween.removeTweens(this.handle);

	}
	//复原小球的位置
	private recoverHandle(evt: egret.TouchEvent) {
		if (evt.touchPointID == this.touchID) {
			this.handle.x = this.circle_position.x;
			this.handle.y = this.circle_position.y;
			this.touchID = null;
			this.pos.x = this.pos.y = 0;
		}
	}
	//复原小球的位置(新手引导)
	public recoverHandle2() {
		this.handle.x = this.circle_position.x;
		this.handle.y = this.circle_position.y;
		this.touchID = null;
		this.pos.x = this.pos.y = 0;
	}
	//小球偏移（x，y）(新手引导)
	public recoverHandle3(x: number, y: number) {
		this.handle.x = this.circle_position.x + x;
		this.handle.y = this.circle_position.y + y;
		this.touchID = null;
		this.pos.x = this.pos.y = 0;
	}
	//获得方向
	public getRotation(): number {
		return this.jiaodu;
	}


	//获得x方向单位时间移动的距离 y方向单位时间移动的距离

	public getPosition() {

		//只去一位小数去掉浮点数的传播

		// this.pos.x = Math.floor((this.handle.x - this.circle_position.x) * 10);
		// this.pos.y = Math.floor((this.handle.y - this.circle_position.y) * 10);

		this.pos.x = this.handle.x - this.circle_position.x;
		this.pos.y = this.handle.y - this.circle_position.y;

		return this.pos;
		//gameMap.vector2_Mouse=pos;
		//console.log("1获取的位置" + this.pos.x + ":" + this.pos.y);
	}

	//给外部发送方向的方法
	public sendPosition() {
		this.getPosition();
		if (webNetworkMananger.isServerMove) {
			// if (!this.gameMapss.isUserDied) {
			// 	this.gameMapss.senMessageForServerPosition(this.pos);
			// }
			return;
		}
		if (DataProl.isDoubleRocker) {
			this.gameMapss.senMessageForServerPosition(this.pos, this.touchLocation);
		} else {
		//	this.gameMapss.senMessageForServerPosition(this.pos);
		}

	}
	/**联网模式发送位置消息 */
	private webSendPosition() {
		if (!this.gameMapss.isUserDied) {
			this.gameMapss.senMessageForServerPosition(this.pos);
		}
	}
	//计算角度 起点到终点
	private calculateRotation(player_position: any, mouse_position: any): number {
		var rotation: number = 0;
		var offestx: number = Math.abs(mouse_position.x - player_position.x);
		var offesty: number = Math.abs(mouse_position.y - player_position.y);
		var alpha: number = Math.atan(offestx / offesty);
		rotation = alpha * 180 / Math.PI;
		if (mouse_position.x - player_position.x > 0 && mouse_position.y - player_position.y > 0) {
			rotation = 180 - rotation;
		} else if (mouse_position.x - player_position.x > 0 && mouse_position.y - player_position.y < 0) {
			rotation = rotation;
		} else if (mouse_position.x - player_position.x < 0 && mouse_position.y - player_position.y < 0) {
			rotation = 360 - rotation;
		} else if (mouse_position.x - player_position.x < 0 && mouse_position.y - player_position.y > 0) {
			rotation = 180 + rotation;
		}
		//console.log(rotation);
		return rotation;
	}
	public clearYaogan() {//清除监听和对象
		// this.timer.removeEventListener(egret.TimerEvent.TIMER, this.sendPosition, this);
		// this.timer.reset();
		if (this.container != null) {
			this.container.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.MoveHandle, this);
			this.container.removeEventListener(egret.TouchEvent.TOUCH_END, this.recoverHandle, this);
			this.container.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.MoveHandle, this);
		}
		if (this.contains(this.handle) && this.contains(this.circle)) {
			this.removeChild(this.handle);
			this.removeChild(this.circle);
		}
	}
}
class Vector_2 {
	public static stage: egret.Stage;
	public x: number = 0;
	public y: number = 0;
	public static normalize(horizontal: number, vertical: number): Vector_2 {
		var unit: Vector_2 = new Vector_2();
		var size: number = Math.sqrt(Math.pow(horizontal, 2) + Math.pow(vertical, 2));
		unit.x = horizontal / size;
		unit.y = vertical / size;
		return unit;
	}
}